.PHONY: run run-web run-h5 dev dev-web dev-h5 build build-web build-h5 clean test docker-up docker-down migrate swagger swagger-web swagger-h5

# 同时启动 Web 和 H5 服务器
# 注意：需要分别打开两个终端窗口运行
run:
	@echo "请使用以下命令分别启动两个服务器："
	@echo "  终端1: make run-web"
	@echo "  终端2: make run-h5"
	@echo "或者使用:"
	@echo "  终端1: go run cmd/web/main.go"
	@echo "  终端2: go run cmd/h5/main.go"

# 启动 Web API 服务器
run-web:
	go run cmd/web/main.go

# 启动 H5 API 服务器
run-h5:
	go run cmd/h5/main.go

# 开发模式（热重载）
# 需要先安装: go install github.com/cosmtrek/air@v1.49.0
dev: dev-web

# 安装 Air（热重载工具）
install-air:
	@echo "📦 安装 Air (v1.49.0)..."
	@go install github.com/cosmtrek/air@v1.49.0 && \
		echo "✅ Air 安装成功！" || \
		echo "❌ Air 安装失败，请检查 Go 环境和网络连接"
	@GOPATH=$$(go env GOPATH); \
	echo "💡 请确保 GOPATH/bin 在 PATH 中:"; \
	echo "   export PATH=\"\$$PATH:$$GOPATH/bin\""

# Web API 热重载
dev-web:
	@GOPATH=$$(go env GOPATH); \
	export PATH="$$PATH:$$GOPATH/bin"; \
	if ! command -v air > /dev/null 2>&1; then \
		echo "📦 Air 未安装，正在安装 (v1.49.0)..."; \
		echo "⏳ 这可能需要几分钟，请稍候..."; \
		go install github.com/cosmtrek/air@v1.49.0 2>&1 && \
		echo "✅ Air 安装成功" || { \
			echo ""; \
			echo "❌ Air 安装失败"; \
			echo "请手动执行: make install-air"; \
			echo "或: go install github.com/cosmtrek/air@v1.49.0"; \
			exit 1; \
		}; \
	fi
	@GOPATH=$$(go env GOPATH); \
	export PATH="$$PATH:$$GOPATH/bin"; \
	echo "🚀 启动 Web API 开发模式（热重载）..."; \
	echo "📝 提示: 修改代码后会自动重新编译和启动"; \
	air -c .air.toml

# H5 API 热重载
dev-h5:
	@GOPATH=$$(go env GOPATH); \
	export PATH="$$PATH:$$GOPATH/bin"; \
	if ! command -v air > /dev/null 2>&1; then \
		echo "📦 Air 未安装"; \
		echo "正在后台安装 Air (v1.49.0)，这可能需要几分钟..."; \
		echo "💡 提示: 如果安装失败，请手动执行: go install github.com/cosmtrek/air@v1.49.0"; \
		go install github.com/cosmtrek/air@v1.49.0 2>&1 && \
		echo "✅ Air 安装成功" || { \
			echo ""; \
			echo "❌ Air 安装失败"; \
			echo "请手动执行: go install github.com/cosmtrek/air@v1.49.0"; \
			echo "然后运行: export PATH=\"\$$PATH:\$$(go env GOPATH)/bin\""; \
			exit 1; \
		}; \
	fi
	@GOPATH=$$(go env GOPATH); \
	export PATH="$$PATH:$$GOPATH/bin"; \
	echo "🚀 启动 H5 API 开发模式（热重载）..."; \
	echo "📝 提示: 修改代码后会自动重新编译和启动"; \
	air -c .air.h5.toml

# 构建所有服务器
build:
	@mkdir -p bin
	go build -o bin/web cmd/web/main.go
	go build -o bin/h5 cmd/h5/main.go

# 构建 Web API 服务器
build-web:
	@mkdir -p bin
	go build -o bin/web cmd/web/main.go

# 构建 H5 API 服务器
build-h5:
	@mkdir -p bin
	go build -o bin/h5 cmd/h5/main.go

clean:
	rm -rf bin/
	rm -rf logs/
	rm -rf docs/
	rm -rf docs-web/
	rm -rf docs-h5/

test:
	go test ./...

docker-up:
	docker-compose up -d

docker-down:
	docker-compose down

migrate:
	go run ./cmd/migrate -cmd=up -config=cmd/web/config.yaml

migrate-status:
	go run ./cmd/migrate -cmd=status -config=cmd/web/config.yaml

migrate-rollback:
	@if [ -z "$(VERSION)" ]; then \
		echo "❌ 错误: 请指定版本号"; \
		echo "使用方法: make migrate-rollback VERSION=001"; \
		exit 1; \
	fi
	@echo "🔄 回滚迁移版本: $(VERSION)"
	@go run ./cmd/migrate -cmd=rollback -version=$(VERSION) -config=cmd/web/config.yaml

migrate-seed:
	@echo "🌱 初始化编号规则种子数据..."
	@go run ./cmd/migrate -cmd=seed -config=cmd/web/config.yaml

migrate-create:
	@if [ -z "$(NAME)" ]; then \
		echo "❌ 错误: 请指定迁移名称或模型名"; \
		echo "使用方法:"; \
		echo "  1. 创建空白迁移: make migrate-create NAME=add_user_avatar"; \
		echo "  2. 基于模型自动生成: make migrate-create NAME=UserModel"; \
		echo "     (会自动识别是 CREATE TABLE 还是 ALTER TABLE)"; \
		exit 1; \
	fi
	@echo "📝 智能创建迁移文件: $(NAME)"
	@go run ./cmd/migrate create $(NAME)

# 生成所有 Swagger 文档
swagger: swagger-web swagger-h5

# 生成 Web API Swagger 文档
swagger-web:
	swag init -g cmd/web/main.go -o docs/web --parseDependency --parseInternal

# 生成 H5 API Swagger 文档
swagger-h5:
	swag init -g cmd/h5/main.go -o docs/h5 --parseDependency --parseInternal

swagger-fmt:
	swag fmt

